package com.zretc.controller;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zretc.entity.HomeInfo;
import com.zretc.entity.PageInfo;
import com.zretc.service.HomestayMessageAdminService;
import com.zretc.service.PageDisplayService;
import com.zretc.service.impl.HomestayMessageAdminServiceImpl;
import com.zretc.service.impl.PageDisplayServiceImpl;

/**
 * @author YuanDali
 * Servlet implementation class HomestayMessageAdminServlet
 */
@WebServlet("/HomestayMessageAdminServlet")
public class HomestayMessageAdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PageDisplayService pageDisplayServiceImpl = new PageDisplayServiceImpl();
	private HomestayMessageAdminService hmAdminService = new HomestayMessageAdminServiceImpl();
	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//获取控制关键字
		String op = request.getParameter("op");
		if ("findAllHomeMessage".equals(op)) {
			//调用获取全部民宿方法
			findAllHomeMessage(request,response);
		}else if("findAllRomeByHomeId".equals(op)) {
			//调用获取指定民宿下的所有房间方法
			findAllRomeByHomeId(request,response);
		}else if("pageHomeMessage".equals(op)) {
			//调用分页获取民宿的方法
			getHomeMessageByPage(request,response);
		}else if("getRoomByCondition".equals(op)) {
			//调用多条件查询房间方法
			getRoomByCondition(request,response);
		}else if("deleteRomeById".equals(op)) {
			//调用通过房间编号删除房间信息方法
			deleteRomeById(request,response);
		}else if("deleteHomeById".equals(op)) {
			//调用通过房间编号删除房间信息方法
			deleteHomeById(request,response);
		}else if("deleteMoreHome".equals(op)) {
			//调用通过民宿编号批量删除民宿信息
			deleteMoreHome(request,response);
		}else if("deleteMoreRoom".equals(op)) {
			//调用通过民宿编号批量删除民宿信息
			deleteMoreRoom(request,response);
		}else if("addRoom".equals(op)) {
			//调用通过民宿编号批量删除民宿信息
			addRoom(request,response);
		}else if("addHome".equals(op)) {
			//调用通过民宿编号批量删除民宿信息
			addHome(request,response);
		}else if("updataRoom".equals(op)) {
			//调用通过民宿编号批量删除民宿信息
			updataRoom(request,response);
		}else if("updataHome".equals(op)) {
			//调用通过民宿编号批量删除民宿信息
			updataHome(request,response);
		}
	}
	private void updataHome(HttpServletRequest request, HttpServletResponse response) throws IOException {
				//获取民宿id
				String homeId = request.getParameter("updataHomeId");
				//获取民宿id
				String homeName = request.getParameter("updataHomeName");
				//获取民宿id
				String homeAddress = request.getParameter("updataHomeAddress");
				//获取民宿id
				String homePhone = request.getParameter("updataHomePhone");
				//获取民宿id
				String homeIntroduce = request.getParameter("updataHomeIntroduce");
				//将信息包装成一个对象
				HomeInfo homeInfo = new HomeInfo(homeId, homeName, homeAddress, homePhone, homeIntroduce);
				//调用业务层添加房间的方法
				int i = hmAdminService.updateHomestay(homeInfo);
				if (i!=1) {
					//添加失败响应一个添加失败提示
					String tip = "更新失败";
					Gson gson = new Gson();
					String jsonTip = gson.toJson(tip);
					response.getWriter().write(jsonTip);
				}else {
					//刷新页面
					findAllHomeMessage(request,response);
				}
	}
	private void updataRoom(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//获取房间编号
				String roomId = request.getParameter("updataRoomId");
				//获取房间状态
				String roomState = request.getParameter("updataRoomState");
				//获取房间价格
				String roomPrice = request.getParameter("updataRoomPrice");
				//获取民宿id
				String homeId = request.getParameter("updataRoomInHomeId");
				//获取房间风格
				String roomStyle = request.getParameter("updataRoomStyle");
				//根据风格获取风格编号
				String styleId = hmAdminService.getStyleIdByStyleName(roomStyle);
				//将信息封装在一个map中
				Map<String, Object> addRoomMap = new HashMap<>();
				addRoomMap.put("roomId", roomId);
				addRoomMap.put("roomState", roomState);
				addRoomMap.put("roomPrice", roomPrice);
				addRoomMap.put("homeId", homeId);
				addRoomMap.put("styleId", styleId);
				System.out.println(addRoomMap);
				//调用业务层的的添加方法
				int i = hmAdminService.updateRoom(addRoomMap);
				//判断是否添加成功
				if(i!=1) {
					//添加失败响应一个添加失败提示
					String tip = "添加失败";
					Gson gson = new Gson();
					String jsonTip = gson.toJson(tip);
					response.getWriter().write(jsonTip);
				}else {
					//调用获取所有房间的方法
					findAllRomeByHomeId(request,response);
				}
	}
	private void addHome(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//获取民宿id
		String homeId = request.getParameter("addHomeId");
		//获取民宿id
		String homeName = request.getParameter("addHomeName");
		//获取民宿id
		String homeAddress = request.getParameter("addHomeAddress");
		//获取民宿id
		String homePhone = request.getParameter("addHomePhone");
		//获取民宿id
		String homeIntroduce = request.getParameter("addHomeIntroduce");
		//获取民宿id
		String homeStyle = request.getParameter("addHomeStyle");
		//根据风格获取风格编号
		String styleId = hmAdminService.getStyleIdByStyleName(homeStyle);
		//将信息包装成一个对象
		HomeInfo homeInfo = new HomeInfo(homeId, homeName, homeAddress, homePhone, homeIntroduce);
		homeInfo.setHomeStyle(styleId);
		System.out.println(homeInfo);
		//调用业务层添加房间的方法
		int i = hmAdminService.addHomestay(homeInfo);
		if (i!=1) {
			//添加失败响应一个添加失败提示
			String tip = "添加失败";
			Gson gson = new Gson();
			String jsonTip = gson.toJson(tip);
			response.getWriter().write(jsonTip);
		}else {
			//刷新页面
			findAllHomeMessage(request,response);
		}
	}
	private void addRoom(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//获取房间编号
		String roomId = request.getParameter("addRoomId");
		//获取房间状态
		String roomState = request.getParameter("addRoomState");
		//获取房间价格
		String roomPrice = request.getParameter("addRoomPrice");
		//获取民宿id
		String homeId = request.getParameter("addRoomInHomeId");
		//获取房间风格
		String roomStyle = request.getParameter("addRoomStyle");
		//根据风格获取风格编号
		String styleId = hmAdminService.getStyleIdByStyleName(roomStyle);
		//将信息封装在一个map中
		Map<String, Object> addRoomMap = new HashMap<>();
		addRoomMap.put("roomId", roomId);
		addRoomMap.put("roomState", roomState);
		addRoomMap.put("roomPrice", roomPrice);
		addRoomMap.put("homeId", homeId);
		addRoomMap.put("styleId", styleId);
		//调用业务层的的添加方法
		int i = hmAdminService.addRoom(addRoomMap);
		//判断是否添加成功
		if(i!=1) {
			//添加失败响应一个添加失败提示
			String tip = "添加失败";
			Gson gson = new Gson();
			String jsonTip = gson.toJson(tip);
			response.getWriter().write(jsonTip);
		}else {
			//调用获取所有房间的方法
			findAllRomeByHomeId(request,response);
		}
	}
	private void deleteMoreRoom(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//获取民宿id格式为Json字符串
		String jsonHomeArrId = request.getParameter("idMessageArr");
		//利用第三方类库将json字符串解析为一个集合
		Gson gson = new Gson();
		Type type = new TypeToken<ArrayList<String>>() {}.getType();
		ArrayList<String> roomIdList = gson.fromJson(jsonHomeArrId, type);
		//调用业务层批量删除的方法
				for (int i = 0; i < roomIdList.size(); i++) {
					//调用业务层删除民宿方法
					hmAdminService.deleteRomeById(roomIdList.get(i));
				}
				//刷新room页面
				findAllRomeByHomeId(request,response);
	}
	private void deleteMoreHome(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//获取民宿id格式为Json字符串
		String jsonHomeArrId = request.getParameter("idMessageArr");
		//利用第三方类库将json字符串解析为一个集合
		Gson gson = new Gson();
		Type type = new TypeToken<ArrayList<String>>() {}.getType();
		ArrayList<String> homeIdList = gson.fromJson(jsonHomeArrId, type);
		//调用业务层删除的方法
		for (int i = 0; i < homeIdList.size(); i++) {
			//调用业务层删除民宿方法
			//调用业务层根据民宿id获取房间的方法
			List<Map<String, String>> roomMessageList = pageDisplayServiceImpl.getRoomMessageByHomestayId(homeIdList.get(i));
			System.out.println(roomMessageList+""+homeIdList);
			if (roomMessageList.size()==0) {
				//如果该民宿下没有房间则删除这个民宿
				 hmAdminService.deleteHomeById(homeIdList.get(i));
			}
			if (i == homeIdList.size()-1) {
				//重新获取全部民宿方法
				findAllHomeMessage(request,response);
			}
		}
	}
	private void deleteHomeById(HttpServletRequest request, HttpServletResponse response) throws IOException {
				//获取民宿id
				String homestayId = request.getParameter("homestayId");
				//根据民宿id判断该民宿下是否有房间
				//调用业务层根据民宿id获取房间的方法
				List<Map<String, String>> roomMessageList = pageDisplayServiceImpl.getRoomMessageByHomestayId(homestayId);
				if (roomMessageList.size()==0) {
					//如果该民宿下没有房间则删除这个民宿
					 hmAdminService.deleteHomeById(homestayId);
					//重新获取全部民宿方法
					 findAllHomeMessage(request,response);
				}else {
					String tip = "请先删除民宿下的房间";
					Gson gson = new Gson();
					String jsonTip = gson.toJson(tip);
					// 返回一个提示语告知该民宿下有房间请先删除房间
					response.getWriter().write(jsonTip);
				}
				
	}
	private void deleteRomeById(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//获取房间id
		String roomId = request.getParameter("roomId");
		//调用业务层通过房间id删除房间信息方法
		int i = hmAdminService.deleteRomeById(roomId);
		if (i!=0) {
			//调用获取指定民宿下的所有房间方法
			findAllRomeByHomeId(request,response);
		}
	}
	private void getRoomByCondition(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//获取民宿id
		String homestayId = request.getParameter("homeId");
		//获取房间id
		String roomId = request.getParameter("roomId");
		//获取最高价
		String maxPrice = request.getParameter("maxPrice");
		//获取房间id
		String minPrice = request.getParameter("minPrice");
		//获取最高价
		String roomStyle = request.getParameter("styleType");
		if (roomId!="") {
			//调用根据房间编号查询房间
			List<Map<String, String>> findRoomMessage = hmAdminService.getRoomMessageById(roomId);
			// 将findRoomMessageList包装成一个gson对象
			Gson gson = new Gson();
			String jsonList = gson.toJson(findRoomMessage);
			// 将json对象响应给ajax请求
			response.getWriter().write(jsonList);
		}else {
			//调用多条件查询房间
			Map<String, String> paramsMap = new HashMap<String, String>();
			paramsMap.put("roomStyle", roomStyle);
			paramsMap.put("inTime", "");
			paramsMap.put("outTime", "");
			paramsMap.put("maxPrice", maxPrice);
			paramsMap.put("minPrice", minPrice);
			paramsMap.put("homestayId", homestayId);
			//调用服务层查询方法
			List<Map<String, String>> findRoomMessageList = pageDisplayServiceImpl.findRoomByCondition(paramsMap);
			System.out.println(findRoomMessageList);
			// 将findRoomMessageList包装成一个gson对象
			Gson gson = new Gson();
			String jsonList = gson.toJson(findRoomMessageList);
			// 将json对象响应给ajax请求
			response.getWriter().write(jsonList);
		}
	}
	private void getHomeMessageByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//获取民宿名称
		String homeName = request.getParameter("homeName");
		//获取民宿名称
		String homeAddress = request.getParameter("homeAddress");
		//获取民宿名称
		String homeStyle = request.getParameter("homeStyle");
		//获取页码和页容量
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		//将获取到的信息装成一个map
		Map<String, String> condition = new HashMap<String, String>();
		condition.put("pageNum", pageNum);
		condition.put("pageSize", pageSize);
		condition.put("homeName", homeName);
		condition.put("homeStyle", homeStyle);
		condition.put("homeAddress", homeAddress);
		System.out.println(condition);
		//调用分页的业务层方法
		PageInfo<Map<String, String>> pageInfo = hmAdminService.findHomestayByCondition(condition);
		//将homestaylist包装成一个gson对象
		Gson gson = new Gson();
		String jsonList = gson.toJson(pageInfo);
		//将json对象响应给ajax请求
		response.getWriter().write(jsonList);
		
	}
	private void findAllRomeByHomeId(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//获取民宿id
		String homestayId = request.getParameter("homestayId");
		//调用业务层根据民宿id获取房间的方法
		List<Map<String, String>> roomMessageList = pageDisplayServiceImpl.getRoomMessageByHomestayId(homestayId);
		//将homestaylist包装成一个gson对象
		Gson gson = new Gson();
		String jsonList = gson.toJson(roomMessageList);
		//将json对象响应给ajax请求
		response.getWriter().write(jsonList);
	}
	private void findAllHomeMessage(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		Map<String, String> condition = new HashMap<String, String>();
		condition.put("pageNum", pageNum);
		condition.put("pageSize", pageSize);
		condition.put("homeName", "");
		condition.put("homeStyle", "选择风格");
		condition.put("homeAddress", "");
		//调用业务层的获取所有民宿信息的方法
		PageInfo<Map<String, String>> pageInfo = hmAdminService.findHomestayByCondition(condition);
		//将homestaylist包装成一个gson对象
		Gson gson = new Gson();
		String jsonList = gson.toJson(pageInfo);
		//将json对象响应给ajax请求
		response.getWriter().write(jsonList);
	}

}
